﻿using Lamex.Base;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lamex.mNC.Common
{
    public class GetCacheTable
    {
        public static DataTable Get_Cache_Table(string PartNo,string DWG_NO, string OrderNumber, double tolerance, DataTable dt_Parameter_Tools, DataTable dt_Parameter_Machine, decimal qty,bool IsCustom,bool IsTest)
        {
            DataTable dt_Result = Common.GetTable.GetTable_CreoPartDetail();
            DataTable dt_Original = new DataTable();
            DataTable dt_Width = new DataTable();
            if (!IsTest)
            {
                dt_Original = Common.GetDataTable.Get_FeatData_PartDetail(PartNo, DWG_NO, "");
                dt_Width = Common.GetDataTable.Get_FeatData_PartDetail(PartNo, DWG_NO, "H1");
            }
            //测试转码专用
            else
            {
                dt_Original = Common.GetDataTable.Get_FeatData_PartDetail_TEST(PartNo, DWG_NO, "");
                dt_Width = Common.GetDataTable.Get_FeatData_PartDetail_TEST(PartNo, DWG_NO, "H1");
            }
            if (dt_Original.Rows.Count > 0)
            {
                //1、复制[FEATURE_TYPE]="TAPP"，并将[FEATURE_TYPE]替换为HOLE，只替换复制的行
                DataRow[] dr_Add = dt_Original.Select("FEATURE_TYPE='TAPP'");
                if (dr_Add.Length > 0)
                {
                    DataTable dt_Add = dr_Add.CopyToDataTable();
                    dt_Add.Columns.Remove("FEATURE_TYPE");//删除列

                    //重新添加列FEATURE_TYPE并赋值
                    DataColumn dc1 = new DataColumn("FEATURE_TYPE", typeof(string));
                    dc1.DefaultValue = "HOLE";//默认值
                    dt_Add.Columns.Add(dc1);

                    //新增临时列FEATURE_DIA_TEMP
                    DataColumn dc_FEATURE_DIA = new DataColumn("FEATURE_DIA_TEMP", typeof(decimal));
                    dt_Add.Columns.Add(dc_FEATURE_DIA);
                    for (int i = 0; i < dt_Add.Rows.Count; i++)
                    {
                        dt_Add.Rows[i]["FEATURE_DIA_TEMP"] = dt_Add.Rows[i]["FEATURE_DIA"].ToString().Length > 0 ?
                           Convert.ToDecimal(dt_Add.Rows[i]["FEATURE_DIA"].ToString()) - 1 : 0;
                    }
                    dt_Add.AcceptChanges();
                    dt_Add.Columns.Remove("FEATURE_DIA");//删除列
                    dt_Add.Columns["FEATURE_DIA_TEMP"].ColumnName = "FEATURE_DIA";//重新调整列名


                    //合并表
                    dt_Original.Merge(dt_Add);
                }
                //2、执行子程序及刀具逻辑，新增字段
                DataColumn dc_SUBROUTINE = new DataColumn("SUBROUTINE", typeof(string));
                dt_Original.Columns.Add(dc_SUBROUTINE);
                DataColumn dc_TOOL_TYPE = new DataColumn("TOOL_TYPE", typeof(string));
                dt_Original.Columns.Add(dc_TOOL_TYPE);
                DataColumn dc_TOOL_DIA = new DataColumn("TOOL_DIA", typeof(double));
                dt_Original.Columns.Add(dc_TOOL_DIA);
                DataColumn dc_TOOL_NO = new DataColumn("TOOL_NO", typeof(string));
                dt_Original.Columns.Add(dc_TOOL_NO);
                DataColumn dc_COMPENSATION_NO = new DataColumn("COMPENSATION_NO", typeof(string));
                dt_Original.Columns.Add(dc_COMPENSATION_NO);

                DataColumn dc_EXPOSED_LENTH = new DataColumn("EXPOSED_LENTH", typeof(string));
                dt_Original.Columns.Add(dc_EXPOSED_LENTH);
                DataColumn dc_ROTATE_SPEED = new DataColumn("ROTATE_SPEED", typeof(string));
                dt_Original.Columns.Add(dc_ROTATE_SPEED);
                DataColumn dc_FEED_SPEED_H = new DataColumn("FEED_SPEED_H", typeof(string));
                dt_Original.Columns.Add(dc_FEED_SPEED_H);
                DataColumn dc_FEED_SPEED_V = new DataColumn("FEED_SPEED_V", typeof(string));
                dt_Original.Columns.Add(dc_FEED_SPEED_V);
                DataColumn dc_BLADE_LENTH = new DataColumn("BLADE_LENTH", typeof(string));
                dt_Original.Columns.Add(dc_BLADE_LENTH);
                DataColumn dc_ERR_INFO = new DataColumn("ERR_INFO", typeof(string));
                dc_ERR_INFO.DefaultValue = "";//默认值
                dt_Original.Columns.Add(dc_ERR_INFO);
                for (int i = 0; i < dt_Original.Rows.Count; i++)
                {
                    string SUBROUTINE = "";
                    string TOOL_TYPE = "";
                    string TOOL_NO = "";
                    string COMPENSATION_NO = "";
                    double TOOL_DIA = 0;
                    string EXPOSED_LENTH = "";
                    string ROTATE_SPEED = "";
                    string FEED_SPEED_H = "";
                    string FEED_SPEED_V = "";
                    string BLADE_LENTH = "";
                    Common.GetParameter_Tools.Parameter_Tools(dt_Parameter_Tools,
                     dt_Original.Rows[i]["FEATURE_TYPE"].ToString(),
                     dt_Original.Rows[i]["FEATURE_DIA"].ToString().Length > 0 ? Convert.ToDouble(dt_Original.Rows[i]["FEATURE_DIA"].ToString()) : 0,
                     dt_Original.Rows[i]["SLOT_LENGTH"].ToString().Length > 0 ? Convert.ToDouble(dt_Original.Rows[i]["SLOT_LENGTH"].ToString()) : 0,
                     dt_Original.Rows[i]["SLOT_WIDTH"].ToString().Length > 0 ? Convert.ToDouble(dt_Original.Rows[i]["SLOT_WIDTH"].ToString()) : 0,
                     dt_Original.Rows[i]["FILLIT_RADIUS"].ToString().Length > 0 ? Convert.ToDouble(dt_Original.Rows[i]["FILLIT_RADIUS"].ToString()) : 0,
                     out SUBROUTINE, out TOOL_TYPE, out TOOL_NO, out COMPENSATION_NO, out TOOL_DIA, out EXPOSED_LENTH,
                     out ROTATE_SPEED, out FEED_SPEED_H, out FEED_SPEED_V, out BLADE_LENTH);

                    DataRow row = dt_Original.Rows[i];
                    row.BeginEdit();
                    row["SUBROUTINE"] = SUBROUTINE;
                    row["TOOL_TYPE"] = TOOL_TYPE;
                    row["TOOL_NO"] = TOOL_NO;
                    row["COMPENSATION_NO"] = COMPENSATION_NO;
                    row["TOOL_DIA"] = TOOL_DIA;
                    row["EXPOSED_LENTH"] = EXPOSED_LENTH;
                    row["ROTATE_SPEED"] = ROTATE_SPEED;
                    row["FEED_SPEED_H"] = FEED_SPEED_H;
                    row["FEED_SPEED_V"] = FEED_SPEED_V;
                    row["BLADE_LENTH"] = BLADE_LENTH;
                    row.EndEdit();

                }
                dt_Original.AcceptChanges();
                //3、根据加工数量循环复制数据                   
                if (dt_Width.Rows.Count > 0)
                {
                    //新增数据类型列QTY_TYPE
                    DataColumn dc_QTY_TYPE = new DataColumn("QTY_TYPE", typeof(string));
                    dt_Original.Columns.Add(dc_QTY_TYPE);
                    decimal MLT = Math.Floor(Convert.ToDecimal(300 / Convert.ToDecimal(dt_Width.Rows[0]["FACE_WIDTH"].ToString())));
                    decimal MOD1 = qty;//Convert.ToDecimal(dt_QTY.Rows[0]["Part_QTY"].ToString());
                    decimal MOD = MOD1 - (Math.Floor(MOD1 / MLT) * MLT);
                    

                    for (int i = 0; i < dt_Original.Rows.Count; i++)
                    {
                        double COORDY = Convert.ToDouble(dt_Original.Rows[i]["COORD_Y"].ToString()) + (tolerance / 2);
                        double FACEWIDTH = Convert.ToDouble(dt_Original.Rows[i]["FACE_WIDTH"].ToString());
                        //【初始高度】【加工深度】
                        string PROCESS_DEPTH = "";//需要逻辑判断
                        string INITIAL_HEIGHT = "";////需要逻辑判断
                        DataRow[] dr_0 = dt_Parameter_Machine.Select("MODULE_CODE='" + dt_Original.Rows[i]["MODULE_CODE"].ToString() + "'");
                        if (dr_0.Length > 0)
                        {
                            //初始高度
                            //取得第一条数据的加工面和深度代码
                            string WORK_SURFACE1 = dt_Original.Rows[i]["WORK_SURFACE"].ToString();
                            string DEEP_CODE1 = dt_Original.Rows[i]["DEEP_CODE"].ToString();
                            //拼接成字段【初始高度】
                            string colums_height = WORK_SURFACE1 + "_FEED_" + DEEP_CODE1;
                            DataTable dtTemp = dr_0.CopyToDataTable();
                            INITIAL_HEIGHT = dtTemp.Rows[0][colums_height].ToString().Trim().Length > 0 ?
                               (Convert.ToDouble(dtTemp.Rows[0][colums_height].ToString()) + 2).ToString() : "";

                            //拼接成字段【加工深度】
                            string colums_depth = WORK_SURFACE1 + "_DEEP_" + DEEP_CODE1;
                            if (dt_Original.Rows[i]["FEATURE_TYPE"].ToString().Trim() == "TAPP")
                                PROCESS_DEPTH = dtTemp.Rows[0][colums_depth].ToString().Trim().Length > 0 ?
                                  (Convert.ToDouble(dtTemp.Rows[0][colums_height].ToString()) - Convert.ToDouble(dtTemp.Rows[0][colums_depth].ToString()) - 4).ToString() : "";
                            else
                                PROCESS_DEPTH = dtTemp.Rows[0][colums_depth].ToString().Trim().Length > 0 ?
                                 (Convert.ToDouble(dtTemp.Rows[0][colums_height].ToString()) - Convert.ToDouble(dtTemp.Rows[0][colums_depth].ToString()) - 2).ToString() : "";

                        }
                        //自定义转码
                        if (IsCustom)
                        {
                            //循环CTM=>BPL
                            for (int k = 1; k <= MLT; k++)
                            {
                                DataRow dr = dt_Result.NewRow();
                                dr["FILE_NAME"] = dt_Original.Rows[i]["FILE_NAME"].ToString();
                                dr["DWG_NO"] = dt_Original.Rows[i]["DWG_NO"].ToString();
                                dr["MODULE_CODE"] = dt_Original.Rows[i]["MODULE_CODE"].ToString();
                                dr["PART_NO"] = dt_Original.Rows[i]["PART_NO"].ToString();
                                dr["PART_LENGTH"] = dt_Original.Rows[i]["PART_LENGTH"].ToString();
                                dr["WORK_SURFACE"] = dt_Original.Rows[i]["WORK_SURFACE"].ToString();
                                dr["FACE_WIDTH"] = FACEWIDTH;
                                dr["FACE_HEIGHT"] = dt_Original.Rows[i]["FACE_HEIGHT"].ToString();
                                dr["UDF_NAME"] = dt_Original.Rows[i]["UDF_NAME"].ToString();
                                dr["FEATURE_TYPE"] = dt_Original.Rows[i]["FEATURE_TYPE"].ToString();
                                dr["DEEP_CODE"] = dt_Original.Rows[i]["DEEP_CODE"].ToString();
                                dr["COORD_X"] = dt_Original.Rows[i]["COORD_X"].ToString();
                                dr["COORD_Y"] = k == 1 ? COORDY.ToString("0.00") : (COORDY + (FACEWIDTH + tolerance) * (k - 1)).ToString("0.00");
                                dr["COORD_Z"] = dt_Original.Rows[i]["COORD_Z"].ToString();
                                dr["FEATURE_DIA"] = dt_Original.Rows[i]["FEATURE_DIA"].ToString().Trim().Length > 0 ? dt_Original.Rows[i]["FEATURE_DIA"].ToString() : null;
                                dr["SLOT_LENGTH"] = dt_Original.Rows[i]["SLOT_LENGTH"].ToString().Trim().Length > 0 ? dt_Original.Rows[i]["SLOT_LENGTH"].ToString() : null;
                                dr["SLOT_WIDTH"] = dt_Original.Rows[i]["SLOT_WIDTH"].ToString().Trim().Length > 0 ? dt_Original.Rows[i]["SLOT_WIDTH"].ToString() : null;
                                dr["FILLIT_RADIUS"] = dt_Original.Rows[i]["FILLIT_RADIUS"].ToString().Trim().Length > 0 ? dt_Original.Rows[i]["FILLIT_RADIUS"].ToString() : null;
                                dr["SUBROUTINE"] = dt_Original.Rows[i]["SUBROUTINE"].ToString();
                                dr["TOOL_TYPE"] = dt_Original.Rows[i]["TOOL_TYPE"].ToString();
                                dr["TOOL_DIA"] = dt_Original.Rows[i]["TOOL_DIA"].ToString();
                                dr["TOOL_NO"] = dt_Original.Rows[i]["TOOL_NO"].ToString();
                                dr["COMPENSATION_NO"] = dt_Original.Rows[i]["COMPENSATION_NO"].ToString();
                                dr["QTY_TYPE"] = "BPL";//CTM=>BPL
                                dr["QTY"] = (int)MLT;
                                dr["EXPOSED_LENTH"] = dt_Original.Rows[i]["EXPOSED_LENTH"].ToString().Trim().Length > 0 ?
                                   dt_Original.Rows[i]["EXPOSED_LENTH"].ToString() : null;
                                dr["ROTATE_SPEED"] = dt_Original.Rows[i]["ROTATE_SPEED"].ToString().Trim().Length > 0 ?
                                    dt_Original.Rows[i]["ROTATE_SPEED"].ToString() : null;
                                dr["FEED_SPEED_H"] = dt_Original.Rows[i]["FEED_SPEED_H"].ToString().Trim().Length > 0 ?
                                   dt_Original.Rows[i]["FEED_SPEED_H"].ToString() : null;
                                dr["FEED_SPEED_V"] = dt_Original.Rows[i]["FEED_SPEED_V"].ToString().Trim().Length > 0 ?
                                    dt_Original.Rows[i]["FEED_SPEED_V"].ToString() : null;
                                dr["BLADE_LENTH"] = dt_Original.Rows[i]["BLADE_LENTH"].ToString().Trim().Length > 0 ?
                                 dt_Original.Rows[i]["BLADE_LENTH"].ToString() : null;
                                dr["INITIAL_HEIGHT"] = INITIAL_HEIGHT.Length > 0 ? INITIAL_HEIGHT : null;
                                dr["PROCESS_DEPTH"] = PROCESS_DEPTH.Length > 0 ? PROCESS_DEPTH : null;
                                dr["ERR_INFO"] = "";
                                dt_Result.Rows.Add(dr);
                            }
                            for (int y = 1; y <= MOD; y++)
                            {
                                DataRow dr = dt_Result.NewRow();
                                dr["FILE_NAME"] = dt_Original.Rows[i]["FILE_NAME"].ToString();
                                dr["DWG_NO"] = dt_Original.Rows[i]["DWG_NO"].ToString();
                                dr["MODULE_CODE"] = dt_Original.Rows[i]["MODULE_CODE"].ToString();
                                dr["PART_NO"] = dt_Original.Rows[i]["PART_NO"].ToString();
                                dr["PART_LENGTH"] = dt_Original.Rows[i]["PART_LENGTH"].ToString();
                                dr["WORK_SURFACE"] = dt_Original.Rows[i]["WORK_SURFACE"].ToString();
                                dr["FACE_WIDTH"] = FACEWIDTH;
                                dr["FACE_HEIGHT"] = dt_Original.Rows[i]["FACE_HEIGHT"].ToString();
                                dr["UDF_NAME"] = dt_Original.Rows[i]["UDF_NAME"].ToString();
                                dr["FEATURE_TYPE"] = dt_Original.Rows[i]["FEATURE_TYPE"].ToString();
                                dr["DEEP_CODE"] = dt_Original.Rows[i]["DEEP_CODE"].ToString();
                                dr["COORD_X"] = dt_Original.Rows[i]["COORD_X"].ToString();
                                dr["COORD_Y"] = y == 1 ? COORDY.ToString("0.00") : (COORDY + (FACEWIDTH + tolerance) * (y - 1)).ToString("0.00");
                                dr["COORD_Z"] = dt_Original.Rows[i]["COORD_Z"].ToString();
                                dr["FEATURE_DIA"] = dt_Original.Rows[i]["FEATURE_DIA"].ToString().Trim().Length > 0 ? dt_Original.Rows[i]["FEATURE_DIA"].ToString() : null;
                                dr["SLOT_LENGTH"] = dt_Original.Rows[i]["SLOT_LENGTH"].ToString().Trim().Length > 0 ? dt_Original.Rows[i]["SLOT_LENGTH"].ToString() : null;
                                dr["SLOT_WIDTH"] = dt_Original.Rows[i]["SLOT_WIDTH"].ToString().Trim().Length > 0 ? dt_Original.Rows[i]["SLOT_WIDTH"].ToString() : null;
                                dr["FILLIT_RADIUS"] = dt_Original.Rows[i]["FILLIT_RADIUS"].ToString().Trim().Length > 0 ? dt_Original.Rows[i]["FILLIT_RADIUS"].ToString() : null;
                                dr["SUBROUTINE"] = dt_Original.Rows[i]["SUBROUTINE"].ToString();
                                dr["TOOL_TYPE"] = dt_Original.Rows[i]["TOOL_TYPE"].ToString();
                                dr["TOOL_DIA"] = dt_Original.Rows[i]["TOOL_DIA"].ToString();
                                dr["TOOL_NO"] = dt_Original.Rows[i]["TOOL_NO"].ToString();
                                dr["COMPENSATION_NO"] = dt_Original.Rows[i]["COMPENSATION_NO"].ToString();
                                dr["QTY_TYPE"] = "BWS";
                                dr["QTY"] = (int)MOD;
                                dr["EXPOSED_LENTH"] = dt_Original.Rows[i]["EXPOSED_LENTH"].ToString().Trim().Length > 0 ?
                                   dt_Original.Rows[i]["EXPOSED_LENTH"].ToString() : null;
                                dr["ROTATE_SPEED"] = dt_Original.Rows[i]["ROTATE_SPEED"].ToString().Trim().Length > 0 ?
                                    dt_Original.Rows[i]["ROTATE_SPEED"].ToString() : null;
                                dr["FEED_SPEED_H"] = dt_Original.Rows[i]["FEED_SPEED_H"].ToString().Trim().Length > 0 ?
                                   dt_Original.Rows[i]["FEED_SPEED_H"].ToString() : null;
                                dr["FEED_SPEED_V"] = dt_Original.Rows[i]["FEED_SPEED_V"].ToString().Trim().Length > 0 ?
                                    dt_Original.Rows[i]["FEED_SPEED_V"].ToString() : null;
                                dr["BLADE_LENTH"] = dt_Original.Rows[i]["BLADE_LENTH"].ToString().Trim().Length > 0 ?
                                 dt_Original.Rows[i]["BLADE_LENTH"].ToString() : null;
                                dr["INITIAL_HEIGHT"] = INITIAL_HEIGHT.Length > 0 ? INITIAL_HEIGHT : null;
                                dr["PROCESS_DEPTH"] = PROCESS_DEPTH.Length > 0 ? PROCESS_DEPTH : null;
                                dr["ERR_INFO"] = "";
                                dt_Result.Rows.Add(dr);
                            }
                        }
                        else
                        {
                            //循环MLT
                            for (int j = 1; j <= MLT; j++)
                            {
                                DataRow dr = dt_Result.NewRow();
                                dr["FILE_NAME"] = dt_Original.Rows[i]["FILE_NAME"].ToString();
                                dr["DWG_NO"] = dt_Original.Rows[i]["DWG_NO"].ToString();
                                dr["MODULE_CODE"] = dt_Original.Rows[i]["MODULE_CODE"].ToString();
                                dr["PART_NO"] = dt_Original.Rows[i]["PART_NO"].ToString();
                                dr["PART_LENGTH"] = dt_Original.Rows[i]["PART_LENGTH"].ToString();
                                dr["WORK_SURFACE"] = dt_Original.Rows[i]["WORK_SURFACE"].ToString();
                                dr["FACE_WIDTH"] = FACEWIDTH;
                                dr["FACE_HEIGHT"] = dt_Original.Rows[i]["FACE_HEIGHT"].ToString();
                                dr["UDF_NAME"] = dt_Original.Rows[i]["UDF_NAME"].ToString();
                                dr["FEATURE_TYPE"] = dt_Original.Rows[i]["FEATURE_TYPE"].ToString();
                                dr["DEEP_CODE"] = dt_Original.Rows[i]["DEEP_CODE"].ToString();
                                dr["COORD_X"] = dt_Original.Rows[i]["COORD_X"].ToString();
                                dr["COORD_Y"] = j == 1 ? COORDY.ToString("0.00") : (COORDY + (FACEWIDTH + tolerance) * (j - 1)).ToString("0.00");
                                dr["COORD_Z"] = dt_Original.Rows[i]["COORD_Z"].ToString();
                                dr["FEATURE_DIA"] = dt_Original.Rows[i]["FEATURE_DIA"].ToString().Trim().Length > 0 ? dt_Original.Rows[i]["FEATURE_DIA"].ToString() : null;
                                dr["SLOT_LENGTH"] = dt_Original.Rows[i]["SLOT_LENGTH"].ToString().Trim().Length > 0 ? dt_Original.Rows[i]["SLOT_LENGTH"].ToString() : null;
                                dr["SLOT_WIDTH"] = dt_Original.Rows[i]["SLOT_WIDTH"].ToString().Trim().Length > 0 ? dt_Original.Rows[i]["SLOT_WIDTH"].ToString() : null;
                                dr["FILLIT_RADIUS"] = dt_Original.Rows[i]["FILLIT_RADIUS"].ToString().Trim().Length > 0 ? dt_Original.Rows[i]["FILLIT_RADIUS"].ToString() : null;
                                dr["SUBROUTINE"] = dt_Original.Rows[i]["SUBROUTINE"].ToString();
                                dr["TOOL_TYPE"] = dt_Original.Rows[i]["TOOL_TYPE"].ToString();
                                dr["TOOL_DIA"] = dt_Original.Rows[i]["TOOL_DIA"].ToString();
                                dr["TOOL_NO"] = dt_Original.Rows[i]["TOOL_NO"].ToString();
                                dr["COMPENSATION_NO"] = dt_Original.Rows[i]["COMPENSATION_NO"].ToString();
                                dr["QTY_TYPE"] = "ZPL";//MLT=>ZPL
                                dr["QTY"] = (int)MLT;
                                dr["EXPOSED_LENTH"] = dt_Original.Rows[i]["EXPOSED_LENTH"].ToString().Trim().Length > 0 ?
                                  dt_Original.Rows[i]["EXPOSED_LENTH"].ToString() : null;
                                dr["ROTATE_SPEED"] = dt_Original.Rows[i]["ROTATE_SPEED"].ToString().Trim().Length > 0 ?
                                    dt_Original.Rows[i]["ROTATE_SPEED"].ToString() : null;
                                dr["FEED_SPEED_H"] = dt_Original.Rows[i]["FEED_SPEED_H"].ToString().Trim().Length > 0 ?
                                   dt_Original.Rows[i]["FEED_SPEED_H"].ToString() : null;
                                dr["FEED_SPEED_V"] = dt_Original.Rows[i]["FEED_SPEED_V"].ToString().Trim().Length > 0 ?
                                    dt_Original.Rows[i]["FEED_SPEED_V"].ToString() : null;
                                dr["BLADE_LENTH"] = dt_Original.Rows[i]["BLADE_LENTH"].ToString().Trim().Length > 0 ?
                                   dt_Original.Rows[i]["BLADE_LENTH"].ToString() : null;
                                dr["INITIAL_HEIGHT"] = INITIAL_HEIGHT.Length > 0 ? INITIAL_HEIGHT : null;
                                dr["PROCESS_DEPTH"] = PROCESS_DEPTH.Length > 0 ? PROCESS_DEPTH : null;
                                dr["ERR_INFO"] = "";
                                dt_Result.Rows.Add(dr);
                            }
                            //循环MOD
                            for (int k = 1; k <= MOD; k++)
                            {
                                DataRow dr = dt_Result.NewRow();
                                dr["FILE_NAME"] = dt_Original.Rows[i]["FILE_NAME"].ToString();
                                dr["DWG_NO"] = dt_Original.Rows[i]["DWG_NO"].ToString();
                                dr["MODULE_CODE"] = dt_Original.Rows[i]["MODULE_CODE"].ToString();
                                dr["PART_NO"] = dt_Original.Rows[i]["PART_NO"].ToString();
                                dr["PART_LENGTH"] = dt_Original.Rows[i]["PART_LENGTH"].ToString();
                                dr["WORK_SURFACE"] = dt_Original.Rows[i]["WORK_SURFACE"].ToString();
                                dr["FACE_WIDTH"] = FACEWIDTH;
                                dr["FACE_HEIGHT"] = dt_Original.Rows[i]["FACE_HEIGHT"].ToString();
                                dr["UDF_NAME"] = dt_Original.Rows[i]["UDF_NAME"].ToString();
                                dr["FEATURE_TYPE"] = dt_Original.Rows[i]["FEATURE_TYPE"].ToString();
                                dr["DEEP_CODE"] = dt_Original.Rows[i]["DEEP_CODE"].ToString();
                                dr["COORD_X"] = dt_Original.Rows[i]["COORD_X"].ToString();
                                dr["COORD_Y"] = k == 1 ? COORDY.ToString("0.00") : (COORDY + (FACEWIDTH + tolerance) * (k - 1)).ToString("0.00");
                                dr["COORD_Z"] = dt_Original.Rows[i]["COORD_Z"].ToString();
                                dr["FEATURE_DIA"] = dt_Original.Rows[i]["FEATURE_DIA"].ToString().Trim().Length > 0 ? dt_Original.Rows[i]["FEATURE_DIA"].ToString() : null;
                                dr["SLOT_LENGTH"] = dt_Original.Rows[i]["SLOT_LENGTH"].ToString().Trim().Length > 0 ? dt_Original.Rows[i]["SLOT_LENGTH"].ToString() : null;
                                dr["SLOT_WIDTH"] = dt_Original.Rows[i]["SLOT_WIDTH"].ToString().Trim().Length > 0 ? dt_Original.Rows[i]["SLOT_WIDTH"].ToString() : null;
                                dr["FILLIT_RADIUS"] = dt_Original.Rows[i]["FILLIT_RADIUS"].ToString().Trim().Length > 0 ? dt_Original.Rows[i]["FILLIT_RADIUS"].ToString() : null;
                                dr["SUBROUTINE"] = dt_Original.Rows[i]["SUBROUTINE"].ToString();
                                dr["TOOL_TYPE"] = dt_Original.Rows[i]["TOOL_TYPE"].ToString();
                                dr["TOOL_DIA"] = dt_Original.Rows[i]["TOOL_DIA"].ToString();
                                dr["TOOL_NO"] = dt_Original.Rows[i]["TOOL_NO"].ToString();
                                dr["COMPENSATION_NO"] = dt_Original.Rows[i]["COMPENSATION_NO"].ToString();
                                dr["QTY_TYPE"] = "ZWS";//MOD=>ZWS
                                dr["QTY"] = (int)MOD;
                                dr["EXPOSED_LENTH"] = dt_Original.Rows[i]["EXPOSED_LENTH"].ToString().Trim().Length > 0 ?
                                   dt_Original.Rows[i]["EXPOSED_LENTH"].ToString() : null;
                                dr["ROTATE_SPEED"] = dt_Original.Rows[i]["ROTATE_SPEED"].ToString().Trim().Length > 0 ?
                                    dt_Original.Rows[i]["ROTATE_SPEED"].ToString() : null;
                                dr["FEED_SPEED_H"] = dt_Original.Rows[i]["FEED_SPEED_H"].ToString().Trim().Length > 0 ?
                                   dt_Original.Rows[i]["FEED_SPEED_H"].ToString() : null;
                                dr["FEED_SPEED_V"] = dt_Original.Rows[i]["FEED_SPEED_V"].ToString().Trim().Length > 0 ?
                                    dt_Original.Rows[i]["FEED_SPEED_V"].ToString() : null;
                                dr["BLADE_LENTH"] = dt_Original.Rows[i]["BLADE_LENTH"].ToString().Trim().Length > 0 ?
                                 dt_Original.Rows[i]["BLADE_LENTH"].ToString() : null;
                                dr["INITIAL_HEIGHT"] = INITIAL_HEIGHT.Length > 0 ? INITIAL_HEIGHT : null;
                                dr["PROCESS_DEPTH"] = PROCESS_DEPTH.Length > 0 ? PROCESS_DEPTH : null;
                                dr["ERR_INFO"] = "";
                                dt_Result.Rows.Add(dr);
                            }
                        }

                    }
                    //排序
                    //DataView dv = new DataView(dt_Result);
                    //dv.Sort = "WORK_SURFACE,SUBROUTINE,TOOL_DIA,COORD_X";
                    //dt_Result = dv.ToTable();
                    //dt_Result.DefaultView.Sort = "WORK_SURFACE,SUBROUTINE,TOOL_DIA,COORD_X";
                    //dt_Result = dt_Result.DefaultView.ToTable();

                    //缓存表插入数据库
                    string delete_sql = @"delete from NC_PN_AL_TEMP001 where FILE_NAME='" + dt_Result.Rows[0]["FILE_NAME"].ToString() + "' ";
                    BaseBuildSql.ExecuteNonQuery(delete_sql);
                    if (BaseBuildSql.ExcuteInsert(dt_Result, "NC_PN_AL_TEMP001"))
                    {
                    }
                }
            }
            return dt_Result;
        }

        #region 测试用
        //public static bool ExcuteInsert(DataTable dt, string TableName)
        //{

        //    try
        //    {
        //        string connString = @"data source=172.23.134.36;initial catalog=LamexMESManager;user id=sa;password=Sa@1050%";
        //        SqlConnection connection = new SqlConnection(connString);
        //        connection.Open();
        //        SqlBulkCopy sqlbulkcopy = new SqlBulkCopy(connection);
        //        //sqlbulkcopy.BulkCopyTimeout = 100;  //超时之前操作完成所允许的秒数
        //        //sqlbulkcopy.BatchSize = dt.Rows.Count;  //每一批次中的行数
        //        sqlbulkcopy.DestinationTableName = TableName;  //服务器上目标表的名称
        //        for (int i = 0; i < dt.Columns.Count; i++)
        //        {
        //            // sqlbulkcopy.ColumnMappings.Add(i, i);  //映射定义数据源中的列和目标表中的列之间的关系
        //            sqlbulkcopy.ColumnMappings.Add(dt.Columns[i].ColumnName, dt.Columns[i].ColumnName);
        //        }
        //        sqlbulkcopy.WriteToServer(dt);  // 将DataTable数据上传到数据表中
        //        connection.Close();
        //        return true;
        //    }
        //    catch(Exception ex)
        //    {
        //        string erro = ex.Message;
        //        return false;
        //    }

        //}
        #endregion
    }
}
